package com.wl.shop.service.impl;

import com.wl.shop.entity.Category;
import com.wl.shop.dao.CategoryDao;
import com.wl.shop.service.CategoryService;
import com.wl.shop.utils.UserUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangl
 * @since 2017-05-24
 */
@Service("categoryService")
@Transactional(readOnly = true)
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
	
	@Autowired
	private CategoryDao categoryDao;
	

	@Override
	public int getMaxSort(String parentId) {
		// TODO Auto-generated method stub
		Integer s = categoryDao.selectMaxSort(parentId);
		return s==null?0:s.intValue();
	}

	@Override
	public void saveOrUpdate(Category category) {
		// TODO Auto-generated method stub
		if(StringUtils.isBlank(category.getId())){
			category.preInsert();
			insert(category);
		}else{
			category.preUpdate();
			updateById(category);
		}
		// 清除用户栏目树缓存
		UserUtils.removeCache(UserUtils.CACHE_TREE_CATEGORY_STRING);
	}

	@Override
	public Page<Category> test(String parentId, Page<Category> page) {
		// TODO Auto-generated method stub
		List<Category> list = categoryDao.listByParentId(parentId, page);
		page.setRecords(list);
		return page;
	}

	@Override
	public void delete(String id) {
		// TODO Auto-generated method stub
		Category category = new Category();
		category.setIsDel(true);
		category.preUpdate();
		update(category, new EntityWrapper<Category>().eq("id", id).or().eq("parent_id", id));
		// 清除用户栏目树缓存
		UserUtils.removeCache(UserUtils.CACHE_TREE_CATEGORY_STRING);
	}

	@Override
	public Category selectCategoryByParam(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return categoryDao.selectCategoryByParam(map);
	}
}
